package com.ruoyi.web.controller.byw;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.BywEmployeeImportInfo;
import com.ruoyi.system.service.IBywEmployeeImportInfoService;
import io.lettuce.core.StrAlgoArgs;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BywEmployee;
import com.ruoyi.system.service.IBywEmployeeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 员工Controller
 * 
 * @author ruoyi
 * @date 2024-05-25
 */
@Api("员工管理")
@RestController
@RequestMapping("/byw/employee")
public class BywEmployeeController extends BaseController
{
    @Autowired
    private IBywEmployeeService bywEmployeeService;

    @Autowired
    private IBywEmployeeImportInfoService employeeImportInfoService;

    /**
     * 查询员工列表
     */
    @ApiOperation("查询")
    @PreAuthorize("@ss.hasPermi('byw:employee:list')")
    @GetMapping("/list")
    public TableDataInfo list(BywEmployee bywEmployee)
    {
        startPage();
        List<BywEmployee> list = bywEmployeeService.selectBywEmployeeList(bywEmployee);
        for (BywEmployee employee : list){
            BywEmployeeImportInfo employeeImportInfo = employeeImportInfoService.selectLastEmployeeImportInfoById(employee.getId());
            if (employeeImportInfo != null){
                employee.setImportTime(employeeImportInfo.getCreateTime());
                employee.setImportViteaNum(employeeImportInfo.getVitaeNum());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出员工列表
     */
    @PreAuthorize("@ss.hasPermi('byw:employee:export')")
    @Log(title = "员工", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BywEmployee bywEmployee)
    {
        List<BywEmployee> list = bywEmployeeService.selectBywEmployeeList(bywEmployee);
        ExcelUtil<BywEmployee> util = new ExcelUtil<BywEmployee>(BywEmployee.class);
        util.exportExcel(response, list, "员工数据");
    }

    /**
     * 获取员工详细信息
     */
    @PreAuthorize("@ss.hasPermi('byw:employee:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bywEmployeeService.selectBywEmployeeById(id));
    }

    /**
     * 新增员工
     */
    @ApiOperation("新增员工")
    @PreAuthorize("@ss.hasPermi('byw:employee:add')")
    @Log(title = "员工", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BywEmployee bywEmployee)
    {
        if (StringUtils.isBlank(bywEmployee.getCellphone())){
            return error("新增员工'" + bywEmployee.getName() + "'失败，手机号为空");
        }
        if (!isPhoneNumberValid(bywEmployee.getCellphone())){
            return error("新增员工'" + bywEmployee.getName() + "'失败，手机号不合法");
        }
        BywEmployee bywEmployee1 = bywEmployeeService.selectEmployeeByPhone(bywEmployee.getCellphone());
        if (bywEmployee1 != null){
            return error("新增员工'" + bywEmployee.getName() + "'失败，手机号已存在");
        }

        bywEmployee.setCreateBy(getUsername());
        bywEmployee.setCreateTime(DateUtils.getNowDate());
        bywEmployee.setUpdateBy(bywEmployee.getCreateBy());
        bywEmployee.setUpdateTime(bywEmployee.getCreateTime());
        return toAjax(bywEmployeeService.insertBywEmployee(bywEmployee));
    }

    public static boolean isPhoneNumberValid(String phoneNumber) {
        // 正则表达式，匹配中国大陆手机号码
        String regex = "^1[3-9]\\d{9}$";
        return phoneNumber.matches(regex);
    }

    /**
     * 修改员工
     */
    @PreAuthorize("@ss.hasPermi('byw:employee:edit')")
    @Log(title = "员工", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BywEmployee bywEmployee)
    {
        return toAjax(bywEmployeeService.updateBywEmployee(bywEmployee));
    }

    /**
     * 删除员工
     */
    @PreAuthorize("@ss.hasPermi('byw:employee:remove')")
    @Log(title = "员工", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bywEmployeeService.deleteBywEmployeeByIds(ids));
    }
}
